<template>
  <comcard>
    <div slot="com_title">添加用户</div>
    <div slot="com_content">
      <el-row>
        <el-col :sm="12">
          <el-form
            :model="ruleForm"
            status-icon
            :rules="rules"
            ref="ruleForm"
            label-width="100px"
          >
            <el-form-item label="账号：" prop="account">
              <el-input type="text" v-model="ruleForm.account"></el-input>
            </el-form-item>
            <el-form-item label="密码：" prop="password">
              <el-input
                type="accountword"
                v-model="ruleForm.password"
                autocomplete="off"
              ></el-input>
            </el-form-item>
            <el-form-item label="用户组：">
              <el-select
                placeholder="请选择用户组"
                v-model="ruleForm.userGroup"
              >
                <el-option value="超级管理员" label超级管理员></el-option>
                <el-option value="普通管理员" label="普通管理员"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submitForm('ruleForm')"
                >提交</el-button
              >
              <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </div>
  </comcard>
</template>

<script>
import { api_save } from "../../api/users";
import Comcard from "../../components/comcard";
export default {
  components: {
    Comcard,
  },
  data() {
    var validateaccount = (rule, value, callback) => {
      const reg = /^[0-9a-z]{3,12}$/gi;
      if (reg.test(value)) {
        this.flag = true;
        callback();
      } else {
        return callback(new Error("请输入3-12位由数字或者字母组成的账号"));
      }
    };
    var validateaccount2 = (rule, value, callback) => {
      const reg = /^[a-z][0-9a-z]{2,11}$/gi;
      if (reg.test(value)) {
        callback();
      } else {
        return callback(
          new Error(
            "第一个字符是字母，其后字符可以是字母 数字 下划线，长度为3-12"
          )
        );
      }
    };
    return {
      ruleForm: {
        account: "",
        password: "",
        userGroup: "",
      },
      flag: false,
      rules: {
        account: [{ validator: validateaccount, trigger: "blur" }],
        password: [{ validator: validateaccount2, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let rst = await api_save(this.ruleForm);
          console.log(rst);
          let { code, msg } = rst;
          if (code == 0) {
            this.$router.push("/users/list");
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style lang="less" scoped>
/deep/.success {
  border-color: green !important;
}
</style>
